﻿using DataSync.Entity;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace DataSync.Proc.CustParse
{
    class SubstrCustParse : ICustParse
    {
        private static ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public void Init(XElement xmlnode)
        {
         
        }

        public string Parse(string curValue, object[] rowValues, string custParams, CustParseExtData extParams)
        {
            string NewValue = curValue;
            try
            {
                var CovParam = JsonConvert.DeserializeObject<SubstrCustParseParams>(custParams);
                if (!string.IsNullOrEmpty(curValue))
                {
                    int index = CovParam.StartIndex;
                    if (!string.IsNullOrEmpty(CovParam.StartStr))
                    {
                        index = curValue.IndexOf(CovParam.StartStr) + CovParam.StartStr.Length;
                    }
                    if (index > curValue.Length)
                    {
                        NewValue = string.Empty;
                    }
                    else
                    {
                        if (CovParam.Length > 0)
                        {
                            NewValue = curValue.Substring(index, CovParam.Length);
                        }
                        else
                        {
                            NewValue = curValue.Substring(index);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Warn(string.Format("格式转换出错:{0}", curValue), ex);
            }
            return NewValue;
        }
    }

    class SubstrCustParseParams
    {
        public string StartStr { get; set; }
        public int StartIndex { get; set; }
        public int Length { get; set; }
    }
}
